package com.example.system.dao;

import com.example.system.entity.User;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

@Mapper
@Repository
public interface UserDAO {

    @Select("select * from tb_user where user_name = #{user_name} and password = #{password}")
    User queryUser(User user);

    @InsertProvider(type = UserDAOProvider.class, method = "saveUser")
    void saveUser(User user);

    @SelectProvider(type = UserDAOProvider.class, method = "queryInfo")
    List<User> queryInfo(User user);

    class UserDAOProvider {
        public String saveUser(User user){
            return  new SQL(){
                {
                    INSERT_INTO("tb_user");

                    if (!StringUtils.isEmpty(user.getUser_name())) {
                        VALUES("user_name"," '"+user.getUser_name()+"'");
                    }
                    if (StringUtils.isEmpty(user.getPassword())) {
                        VALUES("password","123456");
                    }
                    if (!StringUtils.isEmpty(user.getRole())) {
                        VALUES("role"," '"+user.getRole()+"'");
                    }
                    if (StringUtils.isEmpty(user.getRegist_time())) {
                        VALUES("regist_time","now()");
                    }
                    if (!StringUtils.isEmpty(user.getAddress())) {
                        VALUES("address"," '"+user.getAddress()+"'");
                    }
                    if (!StringUtils.isEmpty(user.getTel())) {
                        VALUES("tel"," '"+user.getTel()+"'");
                    }
                    if (!StringUtils.isEmpty(user.getCreator())) {
                        VALUES("creator"," '"+user.getCreator()+"'");
                    }
                    if (!StringUtils.isEmpty(user.getNotes())) {
                        VALUES("notes"," '"+user.getNotes()+"'");
                    }

                }
            }.toString();

        }


        public String queryInfo(User user){
            //结构化sql
           /* FROM和WHERE：跟SELECT一样，可以写多个参数，也可以在多行重复使用，最终会智能合并而不会报错
            这样语句适用于写很长的SQL时，能够保证SQL结构清楚。便于维护，可读性高。*/
            return new SQL(){
                {
                    SELECT("*");
                    FROM("tb_user");
                    if(!StringUtils.isEmpty(user.getUser_name())){
                        WHERE("user_name like '%"+user.getUser_name()+"%' ");
                    }
                    if(!StringUtils.isEmpty(user.getRole())){
                        if(user.getRole().equals("请选择")){

                        }else{
                            WHERE("role like '%"+user.getRole()+"%' ");
                        }
                    }
                    if (!StringUtils.isEmpty(user.getRegist_time()) ){
                        WHERE("regist_time = #{regist_time}");
                    }
                    if (!StringUtils.isEmpty(user.getId())){
                        WHERE("id = #{id}");
                    }
                }
            }.toString();

        }

    }
}
